package com.dayuanit.dy11.mall.mallspringboot.controller;

import com.dayuanit.dy11.mall.mallspringboot.dto.CartJieSuanDTO;
import com.dayuanit.dy11.mall.mallspringboot.dto.ResponseDTO;
import com.dayuanit.dy11.mall.mallspringboot.service.CommoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class CartController extends BaseController {

    @Autowired
    private CommoService commoService;

    @RequestMapping(value = "/addCart", method = RequestMethod.POST)
    public ResponseDTO addCart(int commoId, int number, HttpSession session, HttpServletRequest request) {
        commoService.addCart(commoId, number, getUserId(session));
        return ResponseDTO.success();
    }

    @RequestMapping(value = "/listCart", method = RequestMethod.GET)
    public ResponseDTO listCart(HttpSession session) {
        return ResponseDTO.success(commoService.listCart(getUserId(session)));
    }

    @RequestMapping(value = "/xx", method = RequestMethod.POST)
    public ResponseDTO jiesuan(String xx) {
        System.out.println(xx);
        //11$4027$3_10$4026$1_
        String commoArray[] = xx.split("\\_");
        for (String commoMsg : commoArray) {
            String commoInfo[] = commoMsg.split("\\$");
            if (commoInfo.length != 3) {
                break;
            }
            System.out.println("cartId =" + commoInfo[0] + ", comoId=" + commoInfo[1] + ", buyNum=" + commoInfo[2] );
        }

        return ResponseDTO.success();
    }

    @RequestMapping(value = "/xx2", method = RequestMethod.POST)
    public ResponseDTO jiesuan2(@RequestBody List<CartJieSuanDTO> cartJieSuanDTOS) {

        for (CartJieSuanDTO cartInfo : cartJieSuanDTOS) {
            System.out.println(cartInfo.getBuyNum() + "-" + cartInfo.getCartId() + "-" + cartInfo.getCommoId());
        }
        return ResponseDTO.success();
    }
}
